package com.zhejiangzhengyuan.municipal_common.utils.LED;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskProgressQueryUtils {

    private static String URL = "http://218.75.49.82:9999/ListenSdkService/v1.0.0/deviceTaskUpdateList";

    private static String status_successful = "successful";

    private static String status_Processing = "Processing";

    private static String status_Pending = "Pending";


    public static boolean IsSuccess(String deviceCode, String currentTime) throws ParseException {

        List<Map<String, String>> mapList = TaskProgressQueryUtils.GetTaskProgressList(URL);
        if (mapList != null && mapList.size() > 0) {
            for (int i = 0; i < mapList.size(); i++) {
                Map<String, String> map = mapList.get(i);
                // todo 如果有一个成功就返回
                if (map.get("DeviceCode").equals(deviceCode)
                        && map.get("HandlingStatus").equals(status_successful)) {  //todo 如果设备编号相等，并且状态是成功的

                   if (DateUtils.CompareTime(currentTime,map.get("UpdateTime"))){ //todo 比较时间
                       return true;
                   }
                }
            }
        }
        return false;
    }


    public static List<Map<String, String>> GetTaskProgressList(String url) {
        try {
            Document document = Jsoup.connect(url).get();
            Elements trs = document.select("table").select("tr");
            List<Map<String, String>> mapList = new ArrayList<>();
            for (int i = 0; i < trs.size(); i++) {
                Elements tds = trs.get(i).select("td");
                String s0 = tds.get(0).text();//todo  任务ID
                String s1 = tds.get(1).text();//todo  设备编号
                String s2 = tds.get(2).text();//todo  任务类型
                String s3 = tds.get(3).text();//todo  处理状态
                String s4 = tds.get(4).text();//todo  下载进度
                String s5 = tds.get(5).text();//todo  下载速率
                String s6 = tds.get(6).text();//todo  更新人
                String s7 = tds.get(7).text();//todo  更新时间
                String s8 = tds.get(8).text();//todo  任务描述

                HashMap<String, String> map = new HashMap<>();
                map.put("TaskId", s0);
                map.put("DeviceCode", s1);
                map.put("TaskType", s2);
                map.put("HandlingStatus", s3);
                map.put("DownloadProgress", s4);
                map.put("DownloadSpeed", s5);
                map.put("UpdatePerson", s6);
                map.put("UpdateTime", s7);
                map.put("TaskDescription", s8);

                mapList.add(map);//todo 增加到list
            }
            return mapList;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
